<template>
  <header class="header">
    <div class="header-left">
      <span class="title">智慧地铁大屏</span>
    </div>
    <div class="header-center">
      <span>{{ nowTime }}</span>
      <span class="weather">{{ weather }}</span>
    </div>
  </header>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const nowTime = ref('')
const weather = ref('')

function updateTime() {
  const now = new Date()
  nowTime.value = now.toLocaleString('zh-CN', { hour12: false })
}

onMounted(() => {
  updateTime()
  setInterval(updateTime, 1000)
  getWeather()
})

async function getWeather() {
  try {
    const { data } = await axios.get(
      'https://restapi.amap.com/v3/weather/weatherInfo',
      {
        params: {
          key: import.meta.env.VITE_AMAP_KEY,
          city: '420100', // 武汉
          extensions: 'base',
          output: 'JSON',
        },
      }
    )
    weather.value = data.lives?.[0]?.weather || '未知'
  } catch (e) {
    weather.value = '获取失败'
  }
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #222c3c;
  color: #fff;
  padding: 0 40px;
  height: 60px;
}
.title {
  font-size: 2rem;
  font-weight: bold;
}
.header-center {
  display: flex;
  gap: 24px;
  align-items: center;
}
.weather {
  font-size: 1.1rem;
}
</style>
